<template>
  <div class="login-container">
    <div class="login-bg-container">
      <div class="login-box-container">
        <div class="login-box">
          <div class="login-title-container">
            <div class="login-logo">
              <img class="logo" src="@/assets/image/logo.png" alt="">
            </div>
            <div class="login-title">
              <h2 class="title">Vue2-admin-project</h2>
            </div>
          </div>
          <div class="login-form-container">
            <el-form
              ref="ruleFormRef"
              :model="ruleForm"
              :rules="rules"
            >
              <el-form-item label="" prop="userId">
                <el-input
                  placeholder="请输入用户名"
                  autoComplete="on"
                  style="position: relative"
                  v-model="ruleForm.userId"
                  @keyup.enter.native="submitForm('ruleFormRef')"
                >
                  <i slot="prefix" class="el-input-icon el-icon-user-solid"></i>
                </el-input>
              </el-form-item>

              <el-form-item label="" prop="password">
                <el-input
                  placeholder="请输入密码"
                  autoComplete="on"
                  @keyup.enter.native="submitForm('ruleFormRef')"
                  v-model="ruleForm.password"
                  :type="pwdType"
                >
                  <i slot="prefix" class="el-input-icon el-icon-key"></i>
                  <i slot="suffix" class="el-input-icon el-lock" :class="pwdIcon?'el-icon-lock':'el-icon-unlock'" @click="showPwd"></i>
                </el-input>
              </el-form-item>

              <el-form-item style="width: 100%">
                <el-button
                  :loading="loading"
                  class="login-btn"
                  type="primary"
                  @click="submitForm('ruleFormRef')"
                >登录</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { encryptStr } from "@/utils/jsencrypt";
export default {
  name: 'index',
  data(){
    return{
      loading: false,
      ruleForm: {
        userId: '20230223',
        password: 'liyoyo20230223'
      },
      rules:{
        password: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        userId: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
      pwdIcon: true,
      pwdType: 'password',
    }
  },
  methods:{
    ...mapActions('user',['login','getRoles']),
    submitForm(formName){
      this.loading = true;
      // 密码加密，但是加密一直返回false，不知道哪里出了错
      const password = encryptStr.encrypt(this.ruleForm.password.toString());
      this.$refs[formName].validate(async (valid)=>{
        if(valid){
          await this.login(this.ruleForm)
          .then(async (res) =>{
            await this.getRoles('admin')
            console.log('登录成功  ',res)
            this.loading = false
            this.$router.push({ path: "/" });
          })
          .catch(() =>{
            this.loading = false
            console.log('登录失败')
          })
        }else{
          this.loading = false
        }
      })
    },
    showPwd(){
      this.pwdIcon = !this.pwdIcon
      this.pwdType = this.pwdIcon ? 'password': ''
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container{
  position: relative;
  width: 100%;
  height: 100vh;
  background: #00314c;
  .login-bg-container{
    position: absolute;
    background: white;
    width: calc(100% - 140px);
    height: calc(100vh - 100px);
    top: 50px;
    left: 70px;
    border-radius: 160px 180px 120px 180px/120px 150px 180px 200px;
  }
  .login-box-container{
    width: 100%;
    height: 100%;
    background-image: url("../../assets/login/login-bg.png");
    background-repeat: no-repeat;
    background-size: 100% 100%;
  }
  .login-box{
    position: absolute;
    width: 600px;
    height: 420px;
    border-radius: 20px;
    top: 20%;
    left: 60%;
    background: rgba(237, 239, 240, 0.25);
  }
  .login-title-container{
    height: 80px;
    width: 500px;
    margin-top: 50px;
    margin-left: 50px;
    display: flex;
    .login-logo{
      margin-left: 15px;
      .logo{
        width: 80px;
        height: 80px;
      }
    }
    .login-title{
      margin-left: 50px;
      .title{
        font-size: 36px;
        margin-top: 20px;
      }
    }
  }
  .login-form-container{
    height: 220px;
    width: 500px;
    margin-top: 20px;
    margin-left: 50px;
    .login-btn{
      width: 100%;
    }
  }
  .el-lock{
    cursor: pointer;
  }
}
</style>
